package com.jsonp.service.impl;

import com.jsonp.mapper.FindConditionMapper;
import com.jsonp.service.FindCondition;
import com.jsonp.util.JsoupUtil;
import com.jsonp.vo.JobBean;
import com.jsonp.vo.PageBean;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
@EnableScheduling
public class FindConditionImpl implements FindCondition {
	JsoupUtil jsoupUtil = new JsoupUtil();

	@Autowired
	FindConditionMapper findConditionMapper;
	ObjectMapper mapper;

	@Override
	public String search(String string) throws IOException {
		mapper = new ObjectMapper();
		int number = findConditionMapper.getSearchNum(string);
		String message = "数据收集失败!";
		if (number < 1) {
			int i = findConditionMapper.setJobBean(getJob(string));
			if (i > 0) {
				message = "数据收集完成!";
			}

			return mapper.writeValueAsString(message);
		} else {
			message = "数据已存在!";
			return mapper.writeValueAsString(message);
		}

	}

	@Override
	public List<JobBean> getJob(String string) {
		StringBuffer getUrl = new StringBuffer(
				"https://msearch.51job.com/job_list.php?keyword=");
		getUrl.append(string);
		getUrl.append("&keywordtype=2&funtype=0000&indtype=00&jobarea=040000&workyear=99&jobterm=99&cotype=99&issuedate=9&degree=99&saltype=99&cosize=99&lonlat=&radius=&landmark=0&wxjobid=&filttertype=&pageno=1");
		Document doc;
		try {
			doc = jsoupUtil.getDocument(getUrl.toString());
			int num = jsoupUtil.getPag(doc);
//			String url = jsoupUtil.getPagURL(doc);
			List<String> urlList = jsoupUtil.ModifyURL(String.valueOf(getUrl), num);
			List<JobBean> list = new ArrayList<JobBean>();
			for (int i = 0; i < urlList.size(); i++) {
				Document doc2 = jsoupUtil.getDocument(urlList.get(i));
				try {
					List<JobBean> cycle = jsoupUtil.cycle(doc2, string);
					list.addAll(cycle);

				} catch (ParseException e) {
					e.printStackTrace();
				}

			}
			return list;
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return null;

	}

	@Override
	public String getJobBean(PageBean pageBean) {
		mapper = new ObjectMapper();
		String message = "返回结果小于0!";
		try {
			List<JobBean> list = findConditionMapper.getJobBean(pageBean);
			if (list.size() > 0) {
				message = mapper.writeValueAsString(list);
			}

		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;

	}

	@Override
	public String shieldingCompany(String string) {
		int i = findConditionMapper.shieldingCompany(string);
		String message = "屏蔽失败";
		if (i > 0) {
			message = "屏蔽成功";
		}
		return message;
	}

	@Override
	public String totalPages(String string) {
		mapper = new ObjectMapper();
		int i = findConditionMapper.TotalPages(string);
		try {
			return mapper.writeValueAsString(i);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "数据异常";
	}

	@Override
	// 每五秒执行一次
	@Scheduled(cron = "0 0 */2 * * ?")
	public void RegularlyUpdate() {
		List<String> list = findConditionMapper.getSearch();
		for (int i = 0; i < list.size(); i++) {
			List<String> ls = findConditionMapper.getCompany(list.get(i));
			List<JobBean> list2 = getJob(list.get(i));
			List<JobBean> list3 = new ArrayList<JobBean>();

			for (JobBean jobBean : list2) {
				if (ls.contains(jobBean.getCompany())) {
					list3.add(jobBean);
				}
			}
			list2.removeAll(list3);
			if (list2.size() > 0) {
				int num = findConditionMapper.setJobBean(list2);
				System.out
						.println(list.get(i)
								+ "分组更新了"
								+ num
								+ "条数据----------------------------------------------------------------------------------------");

			} else {
				System.err.println("数据未更新");
			}
		}

	}
}
